package compareResult.datasetCompare;

import com.supermap.data.DatasetGrid;
import com.supermap.data.GeoRegion;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by hanyz on 2016/11/30.
 */
public class DatasetGridComparer extends DatasetComparer{

    public boolean compare(DatasetGrid expDataset, DatasetGrid actualDataset) throws Exception {
        super.compareCommon(expDataset,actualDataset);
        //像素格式
        if (!expDataset.getPixelFormat().equals(actualDataset.getPixelFormat())) {
            compareError += "expDataset PixelFormat:" + expDataset.getPixelFormat() + "\nactualDataset PixelFormat:" + actualDataset.getPixelFormat();
            throw new Exception(compareError);
        }
        //金字塔
        if (expDataset.getHasPyramid() != actualDataset.getHasPyramid()) {
            compareError += "expDataset HasPyramid:" + expDataset.getHasPyramid() + "\nactualDataset HasPyramid:" + actualDataset.getHasPyramid();
            throw new Exception(compareError);
        }
        //像素分块类型
        if(!expDataset.getBlockSizeOption().equals(actualDataset.getBlockSizeOption())){
            compareError += "expDataset BlockSizeOption:" + expDataset.getBlockSizeOption() + "\nactualDataset BlockSizeOption:" + actualDataset.getBlockSizeOption();
            throw new Exception(compareError);
        }
        //极值
        double expValue;
        double actualValue;
        expValue = expDataset.getGridStatisticsResult().getMaxValue();
        actualValue = actualDataset.getGridStatisticsResult().getMaxValue();
        if(!CompareUtilities.compareDouble(expValue,actualValue,tolerance)){
            compareError += "expDataset MaxValue:" + expValue + "\nactualDataset MaxValue:" + actualValue;
            throw new Exception(compareError);
        }
        expValue = expDataset.getGridStatisticsResult().getMinValue();
        actualValue = actualDataset.getGridStatisticsResult().getMinValue();
        if(!CompareUtilities.compareDouble(expValue,actualValue,tolerance)){
            compareError += "expDataset MinValue:" + expValue + "\nactualDataset MinValue:" + actualValue;
            throw new Exception(compareError);
        }
        //空值
        expValue = expDataset.getNoValue();
        actualValue = actualDataset.getNoValue();
        if(!CompareUtilities.compareDouble(expValue,actualValue,tolerance)){
            compareError += "expDataset NoValue:" + expValue + "\nactualDataset NoValue:" + actualValue;
            throw new Exception(compareError);
        }
        //行列数
        if (expDataset.getWidth() != actualDataset.getWidth()) {
            compareError += "expDataset width:" + expDataset.getWidth() + "\nactualDataset width:" + actualDataset.getWidth();
            throw new Exception(compareError);
        }
        if (expDataset.getHeight() != actualDataset.getHeight()) {
            compareError += "expDataset height:" + expDataset.getHeight() + "\nactualDataset height:" + actualDataset.getHeight();
            throw new Exception(compareError);
        }
        //显示范围
        GeoRegion expClipRegion = expDataset.getClipRegion();
        GeoRegion actualClipRegion = actualDataset.getClipRegion();
        EqualAssert.isGeometryEqual(expClipRegion,actualClipRegion,tolerance);
        //比较格子..
        int width = expDataset.getWidth();
        int height = expDataset.getHeight();
        int errorCount = 0;
        int compareCount = 0;
        //网格小于2000顺序间隔比较
        if (width * height < 2000) {
            for (int i = 0; i < width; i += 2) {
                for (int j = 0; j < height; j += 2) {
                    double d1 = expDataset.getValue(i, j);
                    double d2 = actualDataset.getValue(i, j);
                    compareCount++;
                    if (Math.abs(d1 - d2) > tolerance)
                        errorCount++;
                }
            }
        }
        //网格大于2000，随机比较50个点
        else {
            Random random = new Random();
            for (int i = 0; i < 50; i++) {
                int x = random.nextInt(width + 1);
                int y = random.nextInt(height + 1);
                double d1 = expDataset.getValue(x, y);
                double d2 = actualDataset.getValue(x, y);
                compareCount++;
                if (Math.abs(d1 - d2) > tolerance)
                    errorCount++;
            }
        }
        //如果相同值的点大于百分之99则任务对比通过
        if ((double) (errorCount) / (double) compareCount >= 0.01) {
            compareError += "cell diff rate:" + (double) (errorCount) / (double) compareCount;
            throw new Exception(compareError);
        }
        return true;
    }
}
